/**
 * Created by user on 2018/4/25.
 */
import React, { Component } from 'react';
import { connect } from 'react-redux'
import CustomTitle from '../../components/title/CustomTitle'
import { Input, Row, Col, Tag, Card } from 'antd'
import SearchForm from '../../components/weibo/SearchForm'
import SearchContent from '../../components/weibo/SearchContent'
import { Request, GlobalAlert } from '@/utils'
import { API } from '@/config'
import { WeiboActions } from '@/actions'


class Weibo extends Component {
  onSearch = (val) => {
    this.doSearchPromise(val)
  }
  async doSearchPromise(data) {
    const { dispatch } = this.props
    const { setLoading, setUsers, setShow } = WeiboActions
    const { URL, METHOD } = API.SEARCH_WEIBO
    var rp = await Request.SendRequest(URL(), METHOD, data)
    dispatch(setLoading('form', false))
    if (rp.status = Request.RequestStatus.SUCCESS) {
      if (rp.data.code === 200) {
        GlobalAlert.success('login succ')
        dispatch(setShow('users', true))
        dispatch(setUsers(rp.data.data))
      } else {
        GlobalAlert.error(rp.data.msg || 'get fail')
      }
    } else {
      GlobalAlert.error(rp.msg)
    }
  }
  componentWillReceiveProps(nextProps) {
    const { dispatch } = this.props
    const { setInit } = WeiboActions
    console.log('componentWillReceiveProps', nextProps)
    if(nextProps.location.action === 'POP') {
      console.log('nextProps.location.action')
      dispatch(setInit())
    }
  }
  render() {
    console.log('index', this.props)
    return (
      <div className="base_page">
        <CustomTitle bread={['微博', '主页']} title={'微博'} tip={ '搜索微博博主或话题，获取最近评论' } />
        <Card bordered={false} className="base_content">
          <SearchForm onSubmit={this.onSearch}/>
          <SearchContent/>
        </Card>
      </div>
    )
  }
}

const mapState = state => {
  const { WeiboState } = state
  return {}
}

export default connect(mapState)(Weibo)
